package studyguide.ch17;

import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class AlarmShowMidlet extends MIDlet {

	private final Command exitCommand;
	private final Display display;
	
	private final TextBox textBox;
	
	private class SampleCommandListener implements CommandListener {
		public void commandAction(Command command, Displayable displayable) {
			if (command == exitCommand) {
				destroyApp(true);
				notifyDestroyed();
			} else {
				throw new RuntimeException("UNKNOWN COMMAND" + command);
			}
		}
	}
	
	public AlarmShowMidlet() throws IOException {
		textBox = new TextBox("Alarm", "Alarm event!", 100, TextField.UNEDITABLE);
		exitCommand = new Command("Exit", Command.EXIT, 1);
		
		textBox.addCommand(exitCommand);
		
		SampleCommandListener listener = new SampleCommandListener();
		textBox.setCommandListener(listener);
		display = Display.getDisplay(this);
	}
	
	protected void destroyApp(boolean unconditional) {
	}

	protected void pauseApp() {
	}

	protected void startApp() throws MIDletStateChangeException {
		display.setCurrent(textBox);	
	}

}
